package com.zlzxm.shoesmarket.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.zlzxm.shoesmarket.globle.Constant;
import com.zlzxm.shoesmarket.net.BaseCallBack;
import com.zlzxm.shoesmarket.net.repository.GoodsRepository;
import com.zlzxm.shoesmarket.net.respone.RpGoods;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityAddGoodsInfo;
import com.zlzxm.shoesmarket.view.adapter.entity.EntitySearchHistory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * CREATE BY zlzxm ON 4/15/21
 */
public class VMAddGoods extends BaseViewModel {

    private GoodsRepository goodsRepository = new GoodsRepository();

    private int page = 0;

    private String pageSize = "10";

    private MutableLiveData<List<EntityAddGoodsInfo>> liveGoods = new MutableLiveData<>();

    private MutableLiveData<List<EntitySearchHistory>> liveHistory = new MutableLiveData<>();

    public MutableLiveData<Boolean> liveIsLoadSucceed = new MutableLiveData<>();

    public MutableLiveData<Boolean> liveIsLoadEnd = new MutableLiveData<>();

    public MutableLiveData<List<EntityAddGoodsInfo>> getLiveGoods() {
        return liveGoods;
    }

    public MutableLiveData<List<EntitySearchHistory>> getLiveHistory() {
        return liveHistory;
    }



    public VMAddGoods() {

        liveGoods.setValue(new ArrayList<>());
    }

    public void requestGoods(){


        goodsRepository.list("",page+"","10")
                .compose(RxAndroidHelp.turn())
                .subscribe(new BaseCallBack<RpGoods>() {
                    @Override
                    protected void succeed(String msg, RpGoods s) {
                        liveGoods.getValue().addAll(createGoods(s.getGoodsList()));
                        liveGoods.setValue(liveGoods.getValue());
                        if(s.getGoodsList().size()<10){

                            liveIsLoadEnd.setValue(true);
                            return;
                        }

                        if(page>0){
                            liveIsLoadSucceed.setValue(true);
                        }
                        page++;
                    }


                    @Override
                    protected void failed(String msg) {
                        liveErrorToast.setValue(msg);
                        if(page > 0){
                            liveIsLoadSucceed.setValue(false);
                        }
                    }

                    @Override
                    protected void notLogin() {

                    }
                },throwableCallback);


    }


    public void requestHistory(){


        Observable.timer(10, TimeUnit.SECONDS)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {


                        liveHistory.setValue(createHistory());

                    }
                });



    }

//<editor-fold defaultState="collapsed" desc="测试">

    private List<EntityAddGoodsInfo> createGoods(List<RpGoods.Entity> source){

        List<EntityAddGoodsInfo> list = new ArrayList<>();

        for (int i = 0; i < source.size(); i++) {

            EntityAddGoodsInfo entityAddGoodsInfo =  new EntityAddGoodsInfo();
            entityAddGoodsInfo.setDesc(source.get(i).getArticleNumber());
            entityAddGoodsInfo.setTitle(source.get(i).getTitle());
            entityAddGoodsInfo.setFace(source.get(i).getLogoUrl());
            entityAddGoodsInfo.setCode(source.get(i).getArticleNumber());
            entityAddGoodsInfo.setBrand(source.get(i).getBrandName());
            entityAddGoodsInfo.setTags(source.get(i).getPsize());


            list.add(entityAddGoodsInfo);

        }

        return  list;

    }


    private List<EntitySearchHistory> createHistory(){


        List<EntitySearchHistory> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            EntitySearchHistory searchHistory = new EntitySearchHistory();
            searchHistory.setTitle("qqe");

            list.add(searchHistory);

        }

        return  list;


    }


// </editor-fold>
}
